﻿using System;
using System.Windows.Input;

namespace CuteRibs.MVVMLib
{
	public class DelegateCommand : ICommand
	{
		public event EventHandler CanExecuteChanged;
		private Func<object, bool> canExecute;
		private Action<object> executeAction;

		public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute = null)
		{
			if (executeAction == null)
			{
				throw new ArgumentNullException("executeAction");
			}

			this.executeAction = executeAction;
			this.canExecute = canExecute;
		}

		public bool CanExecute(object parameter)
		{
			bool result = true;
			Func<object, bool> canExecuteHandler = this.canExecute;

			if (canExecuteHandler != null)
			{
				result = canExecuteHandler(parameter);
			}

			return result;
		}

		public void Execute(object parameter)
		{
			this.executeAction(parameter);
		}
	}
}
